// pages/weather/weather.js
const app = getApp();
const util = require('../../utils/util')
Page({
    /**
     * 页面的初始数据 
     */
    data: {
        // region: ['河北省',getApp().data.localCity,'裕华区'],
        region: getApp().data.localCity,
        today:'',
        tomorrow:'',
        aftertomorrow:'',
        date:util.formatDate(new Date())
    },
    change:function(e){
        this.setData({
          region: e.detail.value
          // region: getApp().data.localCity
        })
        this.getWeather();
    },
    getWeather:function(e){
        var that = this;
        this.setData({
          region:getApp().data.localCity
        })
        wx.request({
            url:'https://geoapi.qweather.com/v2/city/lookup?',
            data:{
                // location:that.data.region[2],
                // adm:that.data.region[1],
                location: that.data.region+'市',
                key:'ba559625423d42cf966fa216ddb073f0'
            },
            success (res) {
                console.log(res.data.location[0].id);
              wx.request({
                url: 'https://devapi.qweather.com/v7/weather/3d?',
                data:{
                //   location: 101010100,
                  location: res.data.location[0].id,
                  key:'ba559625423d42cf966fa216ddb073f0'
                },
                success:function(res){
                  // console.log(res.data.daily[0])
                  console.log(that.data.region);
                  that.setData({ 
                    today:res.data.daily[0],
                    tomorrow:res.data.daily[1],
                    aftertomorrow:res.data.daily[2]
                  })
                  let today = res.data.daily[0];
                  let tomorrow = res.data.daily[1];
                  let aftertomorrow = res.data.daily[2];
                  console.log(today.textDay);
                  // console.log(tomorrow.textDay);
                  // console.log(aftertomorrow.textDay);
                  wx.request({
                    url: `https://wx.qasdwer.top:2001/weather?textDay=${today.textDay}`,
                    method: 'GET',
                    dataType: 'json', 
                    success: res=>{
                      console.log(res.data);
                      that.setData({
                        src1:res.data[0].image
                      })
                    }
                  })
                  wx.request({
                    url: `https://wx.qasdwer.top:2001/weather?textDay=${tomorrow.textDay}`,
                    method: 'GET',
                    dataType: 'json', 
                    success: res=>{
                      console.log(res.data);
                      that.setData({
                        src2:res.data[0].image
                      })
                    }
                  })
                  wx.request({
                    url: `https://wx.qasdwer.top:2001/weather?textDay=${aftertomorrow.textDay}`,
                    method: 'GET',
                    dataType: 'json', 
                    success: res=>{
                      console.log(res.data);
                      that.setData({
                        src3:res.data[0].image
                      })
                    }
                  })

                }
              })
            }
        })
      },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getWeather();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
      this.setData({
        date:util.formatDate(new Date()),
        shidu:'/icon/shidu.png',
        qiya:'/icon/qiya.png',
        nengjiandu:'/icon/nengjiandu.png',
        fengxiang:'/icon/fengxiang_1.png',
        fengsu:'/icon/fengsu.png',
        fengli:'/icon/fengli.png'
      })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})